#! /usr/bin/env python
# -*- coding: iso-8859-1 -*-
##############################################################################
#
#
#         ___           ___           ___           ___
#        /  /\         /  /\         /  /\         /  /\
#       /  /::\       /  /:/_       /  /:/_       /  /::\
#      /  /:/\:\     /  /:/ /\     /  /:/ /\     /  /:/\:\
#     /  /:/~/:/    /  /:/ /:/_   /  /:/ /::\   /  /:/~/:/
#    /__/:/ /:/___ /__/:/ /:/ /\ /__/:/ /:/\:\ /__/:/ /:/
#    \  \:\/:::::/ \  \:\/:/ /:/ \  \:\/:/~/:/ \  \:\/:/
#     \  \::/~~~~   \  \::/ /:/   \  \::/ /:/   \  \::/
#      \  \:\        \  \:\/:/     \__\/ /:/     \  \:\
#       \  \:\        \  \::/        /__/:/       \  \:\
#        \__\/         \__\/         \__\/         \__\/
#
#
#
#
#   This file is part of ReSP.
#
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
#
#   (c) Giovanni Beltrame, Luca Fossati
#       Giovanni.Beltrame@esa.int fossati@elet.polimi.it
#
##############################################################################


import Options
import sys,os

def build(bld):

    # Build and compile the TLM wrapper extension module
    obj = bld.new_task_gen('pypp')

    obj.source ='''
    tlm_export.hpp
    '''

    obj.target = 'tlmwrapper'
    obj.start_decls = 'tlm tlm_utils'

    obj.custom_code = """
# Exclude protected members
cls = mb.global_ns.calldefs( declarations.access_type_matcher_t( 'protected' ) , allow_empty=True )
for i in cls:
    i.exclude()

tlm_enums = mb.global_ns.enumerations(lambda decl: decl.name.startswith('tlm_'))
for i in tlm_enums:
    i.include()

tlm_dmi = mb.global_ns.class_('tlm_dmi')
tlm_dmi.include()
tlm_dmi.member_functions(allow_empty=True).exclude()
tlm_extension_base = mb.global_ns.class_('tlm_extension_base')
tlm_extension_base.include()
tlm_extension_base.member_functions(allow_empty=True).exclude()
tlm_phase = mb.global_ns.class_('tlm_phase')
tlm_phase.include()
tlm_phase.member_functions(allow_empty=True).exclude()
tlm_endian_context = mb.global_ns.class_('tlm_endian_context')
tlm_endian_context.include()
tlm_endian_context.member_functions(allow_empty=True).exclude()
tlm_mm_interface = mb.global_ns.class_('tlm_mm_interface')
tlm_mm_interface.include()
tlm_mm_interface.member_functions(allow_empty=True).exclude()
tlm_extension_base = mb.global_ns.class_('tlm_extension_base')
tlm_extension_base.include()
tlm_extension_base.member_functions(allow_empty=True).exclude()

# Include simple sockets
#print 'Including base sockets sockets'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('tlm_base_target_socket') or decl.name.startswith('tlm_target_socket') , allow_empty=True)
for i in cls:
    i.include()
    for k in i.member_functions(allow_empty=True):
        k.exclude()
    for k in i.member_operators(allow_empty=True):
        k.exclude()

#print 'Including tlm_base_initiator_socket'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('tlm_base_initiator_socket') or decl.name.startswith('tlm_initiator_socket'), allow_empty=True)
for i in cls:
    i.include()
    i.member_functions(allow_empty=True).exclude()
    for k in i.member_operators(allow_empty=True):
        k.exclude()
    for k in i.member_functions(allow_empty=True):
        if k.name == 'bind':
            k.include()

#print 'Including simple_'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('simple_'), allow_empty=True)
for i in cls:
    i.include()

#print 'Including multi_'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('multi_'), allow_empty=True)
for i in cls:
    i.ignore = False

#print 'Including tlm_base_protocol_types'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('tlm_base_protocol_types'), allow_empty=True)
for i in cls:
    i.include()

#print 'Including tlm_quantumkeeper'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('tlm_quantumkeeper'), allow_empty=True)
for i in cls:
    i.include()

#print 'Including tlm_fw_transport_if'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('tlm_fw_transport_if'), allow_empty=True)
for i in cls:
    i.include()
    for k in i.member_operators(allow_empty=True):
        k.exclude()

#print 'Including tlm_bw_transport_if'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('tlm_bw_transport_if'), allow_empty=True)
for i in cls:
    i.include()
    for k in i.member_operators(allow_empty=True):
        k.exclude()

#print 'Including tlm_generic_payload'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('tlm_generic_payload'), allow_empty=True)
for i in cls:
    i.include()
    for k in i.member_functions(allow_empty=True):
        k.exclude()

#print 'Including multi_passthrough'
cls = mb.global_ns.classes(lambda decl: decl.name.startswith('multi_passthrough'), allow_empty=True)
for i in cls:
    i.include()
    for k in i.member_functions(allow_empty=True):
        k.exclude()
        if k.name == 'bind':
            k.include()
        if k.name == 'size':
            k.include()

tlm_names = mb.namespaces(lambda x: x.name in ['tlm', 'tlm_utils'])
for cur_namespace in tlm_names:
    for i in cur_namespace.free_functions(allow_empty=True):
        if not i.alias.startswith('TLM_'):
            i.alias = 'TLMW_'+i.alias
    for i in cur_namespace.casting_operators(allow_empty=True):
        if not i.alias.startswith('TLM_'):
            i.alias = 'TLMW_'+i.alias
    for i in cur_namespace.classes(allow_empty=True):
        if not i.alias.startswith('TLM_'):
            i.alias = 'TLMW_'+i.alias
    for i in cur_namespace.enumerations(allow_empty=True):
        if not i.alias.startswith('TLM_'):
            i.alias = 'TLMW_'+i.alias
    for i in cur_namespace.variables(allow_empty=True):
        if not i.alias.startswith('TLM_'):
            i.alias = 'TLMW_'+i.alias
    for i in cur_namespace.free_operators(allow_empty=True):
        if not i.alias.startswith('TLM_'):
            i.alias = 'TLMW_'+i.alias
    for i in cur_namespace.typedefs(allow_empty=True):
        if not i.alias.startswith('TLM_'):
            i.alias = 'TLMW_'+i.alias
"""
    obj.uselib = 'SYSTEMC_H TLM BOOST_PYTHON BOOST'
    obj.uselib_local = 'systemc'
    obj.includes = '.'
    obj.include = 'scwrapper'
    obj.templates = [
        'tlm::tlm_fw_transport_if<tlm::tlm_base_protocol_types>'
        ,'tlm::tlm_bw_transport_if<tlm::tlm_base_protocol_types>'
        ,'tlm::tlm_base_target_socket_b<8, tlm::tlm_fw_transport_if<tlm::tlm_base_protocol_types>, tlm::tlm_bw_transport_if<tlm::tlm_base_protocol_types> >'
        ,'tlm::tlm_base_initiator_socket_b<8, tlm::tlm_fw_transport_if<tlm::tlm_base_protocol_types>, tlm::tlm_bw_transport_if<tlm::tlm_base_protocol_types> >'
        ,'tlm::tlm_base_target_socket_b<32, tlm::tlm_fw_transport_if<tlm::tlm_base_protocol_types>, tlm::tlm_bw_transport_if<tlm::tlm_base_protocol_types> >'
        ,'tlm::tlm_base_initiator_socket_b<32, tlm::tlm_fw_transport_if<tlm::tlm_base_protocol_types>, tlm::tlm_bw_transport_if<tlm::tlm_base_protocol_types> >'
    ]
